package com.zhanggm.s3.house.controller;


import com.zhanggm.s3.house.bo.EstateInfoBo;
import com.zhanggm.s3.house.entity.Estate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.zhanggm.s3.core.utils.AjaxResult;

import com.zhanggm.s3.house.service.IEstateService;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2023-07-21
 */
@RestController
@RequestMapping("/estate")
public class EstateController {

    @Autowired
    private IEstateService estateService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 楼盘查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public AjaxResult list(Integer pageNum,Integer pageSize) {
        return AjaxResult.success(estateService.list());
    }

    /**
     * 楼盘排名
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("rank")
    public AjaxResult rank(Integer pageNum,Integer pageSize) {
        return AjaxResult.success(estateService.rankList(pageNum,pageSize));
    }

    /**
     * 楼盘保存接口
     * @param estate
     * @return
     */
    public AjaxResult add(Estate estate) {
        estateService.save(estate);
        return AjaxResult.success();
    }


    /**
     * 楼盘查询接口
     * 接口请求时，缓存数据
     * @return
     */
    @GetMapping("getListFromRedis")
    public synchronized AjaxResult getListFromRedis() {
        Object estateList = redisTemplate.opsForValue().get("estateList");
        if (estateList == null) {
            List<Estate> list = estateService.list();
            //缓存1小时
            redisTemplate.opsForValue().set("estateList",list,60*60,TimeUnit.SECONDS);
            return AjaxResult.success(list);
        }
        return AjaxResult.success(estateList);
    }


    /**
     * 根据楼盘Id，查询商圈地铁信息
     * @param id
     * @return
     */
    @GetMapping("getEstateInfo")
    public AjaxResult getEstateInfo(Integer id) {
        Object estateList = redisTemplate.opsForValue().get("estateList");
        EstateInfoBo estateInfo = estateService.getEstateInfo(id);
        return AjaxResult.success(estateInfo);
    }





}
